package sz.szsmk.citizencard.util

import android.app.Activity
import android.graphics.Color
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.view.View
import com.chenenyu.router.Router

/**
 * Copyright (c)2021/5/26
 * author: hxh
 * created on: 2021/5/26 16:50
 * description:
 */
class HtmlToFont {
    /**
     * html  带有<font>标签的文本
     */
     fun getClickableHtml(html: String?, context: Activity?): CharSequence {
        val spannedHtml: Spanned = Html.fromHtml(html)
        val clickableHtmlBuilder = SpannableStringBuilder(spannedHtml)
        val spans = clickableHtmlBuilder.getSpans(0, spannedHtml.length, URLSpan::class.java)
        for (value in spans) {
            setLinkClickable(clickableHtmlBuilder, value, context);
        }

        return clickableHtmlBuilder
    }



    /**
     * 捕获<a>标签点击事件
     */
     fun setLinkClickable(clickableHtmlBuilder: SpannableStringBuilder, urlSpan: URLSpan?, context: Activity?) {
        val start = clickableHtmlBuilder.getSpanStart(urlSpan)
        val end = clickableHtmlBuilder.getSpanEnd(urlSpan)
        val flags = clickableHtmlBuilder.getSpanEnd(urlSpan)
        val clickableSpan = object : ClickableSpan() {
            override fun onClick(p0: View) {
                val url = urlSpan?.url
                if (url?.contains("serviceAgreement")!!) {

                    //跳转至web页面
                    Router.build("webView").with("url", "https://newapp.szsmk.com/app-front-end/#/common/serviceAgreement").go(context)
                   // JumpUtils.jump(context, "", 0)
                    //mPrivacyDialogListener?.agreementOonClick(url)
                } else {
                    Router.build("webView").with("url", "https://newapp.szsmk.com/app-front-end/#/common/secretAgreement").go(context)
                    //JumpUtils.jump(context, "", 0)
                    //mPrivacyDialogListener?.serviceOnClick(url)
                }
            }

            override fun updateDrawState(ds: TextPaint) {
                //设置颜色
                ds.color = Color.parseColor("#FF9800")
                //设置是否要下划线
                ds.isUnderlineText = false
            }

        }
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);

    }


}